package com.wang.lottery.interfaces.facade;

import com.alibaba.fastjson.JSON;
import com.wang.lottery.application.process.IActivityProcess;
import com.wang.lottery.application.process.req.DrawProcessReq;
import com.wang.lottery.application.process.res.DrawProcessResult;
import com.wang.lottery.application.process.res.RuleQuantificationCrowdResult;
import com.wang.lottery.common.Constants;
import com.wang.lottery.domain.rule.model.req.DecisionMatterReq;
import com.wang.lottery.rpc.ILotteryActivityBooth;
import com.wang.lottery.rpc.dto.AwardDTO;
import com.wang.lottery.rpc.req.DrawReq;
import com.wang.lottery.rpc.req.QuantificationDrawReq;
import com.wang.lottery.rpc.res.DrawRes;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;

/**
 * @author wjh
 * @create 2023-11-12 14:44
 * 抽奖活动展台
 */
@Service
@Slf4j
public class LotteryActivityBooth implements ILotteryActivityBooth {
    
    @Resource
    private IActivityProcess activityProcess;
    
    /**
     * 指定活动抽奖
     *
     * @param drawReq 抽奖请求
     * @return 抽奖结果
     */
    @Override
    public DrawRes doDraw(DrawReq drawReq) {
        DrawProcessReq drawProcessReq = new DrawProcessReq();
        BeanUtils.copyProperties(drawReq, drawProcessReq);
        try {
            log.info("抽奖，开始 uId：{} activityId：{}", drawReq.getuId(), drawReq.getActivityId());
            DrawProcessResult drawProcessResult = activityProcess.doDrawProcess(drawProcessReq);
            if (drawProcessResult!= null && Constants.ResponseCode.SUCCESS.getCode().equals(drawProcessResult.getCode())) {
                DrawRes drawRes = new DrawRes(drawProcessResult.getCode(),drawProcessResult.getInfo());
                drawRes.setAwardDTO(new AwardDTO());
                BeanUtils.copyProperties(drawProcessResult.getDrawAwardInfo(), drawRes.getAwardDTO());
                drawRes.getAwardDTO().setUserId(drawReq.getuId());
                log.info("抽奖，完成 uId：{} activityId：{} drawRes：{}", drawReq.getuId(), drawReq.getActivityId(), JSON.toJSONString(drawRes));

                return drawRes;
            }
            log.error("抽奖，失败(抽奖过程异常) uId：{} activityId：{}", drawReq.getuId(), drawReq.getActivityId());
            return new DrawRes(Constants.ResponseCode.LOSING_DRAW.getCode(),Constants.ResponseCode.LOSING_DRAW.getInfo());
        } catch (Exception e) {
            log.error("抽奖，失败(抽奖过程异常) uId：{} activityId：{}", drawReq.getuId(), drawReq.getActivityId(), e);
            return new DrawRes(Constants.ResponseCode.LOSING_DRAW.getCode(),Constants.ResponseCode.LOSING_DRAW.getInfo());
        }
    }

    /**
     * 量化人群抽奖
     *
     * @param quantificationDrawReq 抽奖请求
     * @return 抽奖结果
     */
    @Override
    public DrawRes doQuantificationDraw(QuantificationDrawReq quantificationDrawReq) {
        DecisionMatterReq decisionMatterReq = new DecisionMatterReq();
        BeanUtils.copyProperties(quantificationDrawReq,decisionMatterReq);
        log.info("量化人群抽奖，开始 uId：{} treeId：{}", quantificationDrawReq.getuId(), quantificationDrawReq.getTreeId());
        try {
            RuleQuantificationCrowdResult result = activityProcess.doRuleQuantificationCrowd(decisionMatterReq);
            DrawProcessResult drawProcessResult = activityProcess.doDrawProcess(new DrawProcessReq(quantificationDrawReq.getuId(), result.getActivityId()));
            if (drawProcessResult!= null && Constants.ResponseCode.SUCCESS.getCode().equals(drawProcessResult.getCode())) {
                DrawRes drawRes = new DrawRes(drawProcessResult.getCode(),drawProcessResult.getInfo());
                drawRes.setAwardDTO(new AwardDTO());
                BeanUtils.copyProperties(drawProcessResult.getDrawAwardInfo(), drawRes.getAwardDTO());
                drawRes.getAwardDTO().setUserId(quantificationDrawReq.getuId());
                return drawRes;
            }
            log.error("量化人群抽奖，失败(抽奖过程异常) uId：{} treeId：{}", quantificationDrawReq.getuId(), quantificationDrawReq.getTreeId());
            return new DrawRes(Constants.ResponseCode.LOSING_DRAW.getCode(),Constants.ResponseCode.LOSING_DRAW.getInfo());
        } catch (Exception e) {
            log.error("量化人群抽奖，失败(抽奖过程异常) uId：{} treeId：{}", quantificationDrawReq.getuId(), quantificationDrawReq.getTreeId(), e);
            return new DrawRes(Constants.ResponseCode.LOSING_DRAW.getCode(),Constants.ResponseCode.LOSING_DRAW.getInfo());
        }
    }
}
